;;; Clight package, dependencies and extending packages for GNU Guix.
;;; Copyright © 2021 florhizome <florhizome@posteo.net>
;;; Copyright © 2022 jgart <jgart@dismail.de>
;;;
;;; This file is NOT part of GNU Guix.
;;;
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

(define-module (guixrus packages clight)
  #:use-module (guix utils)
  #:use-module (guix build utils)
  #:use-module ((guix licenses) #:prefix license:)
  #:use-module (guix packages)
  #:use-module (guix download)
  #:use-module (gnu packages)
  #:use-module (guix git-download)
  #:use-module (guix build-system gnu)
  #:use-module (guix build-system cmake)
  #:use-module (guix build-system qt)
  #:use-module (gnu packages qt)
  #:use-module (gnu packages base)
  #:use-module (gnu packages hardware)
  #:use-module (gnu packages maths)
  #:use-module (gnu packages hardware)
  #:use-module (gnu packages textutils)
  #:use-module (gnu packages freedesktop)
  #:use-module (gnu packages popt)
  #:use-module (gnu packages xorg)
  #:use-module (gnu packages xdisorg)
  #:use-module (gnu packages libusb)
  #:use-module (gnu packages image)
  #:use-module (gnu packages linux)
  #:use-module (gnu packages polkit)
  #:use-module (gnu packages glib)
  #:use-module (gnu packages gnome)
  #:use-module (gnu packages pkg-config)
  #:use-module (gnu packages gcc)
  #:use-module (gnu packages bash))

;;; TODO: build: tests?
;;; Service's to actually use it

(define-public libmodule
  (package
   (name "libmodule")
   (version "5.0.1")
   (source
    (origin
      (method git-fetch)
      (uri (git-reference
            (url "https://github.com/FedeDP/libmodule")
            (commit version)))
      (file-name (git-file-name name version))
      (sha256
       (base32 "1j48gkj6zlifrmx9qh37ijmqjvdvgmpvxah7j2qlrhdxcc6n4i62"))))
   (build-system cmake-build-system)
   (native-inputs `(("pkg-config" ,pkg-config)))
   (arguments '(#:tests? #f)) ; tests not found
   (home-page "https://github.com/FedeDP/libmodule")
   (synopsis "C simple implementation of an actor library")
   (description "Libmodule offers a small and simple C implementation
 of an actor library that aims to let developers easily create modular
 C projects in a way which is both simple and elegant.")
   (license license:gpl3)))

(define-public clight
  (package
   (name "clight")
   (version "4.7")
   (source
    (origin
      (method git-fetch)
      (uri (git-reference
            (url "https://github.com/FedeDP/clight")
            (commit version)))
      (file-name (git-file-name name version))
      (sha256
       (base32 "0i44vr5qn022cd1jb2rd9hg50jy0yk82jnl1kznf1jfli9g79vps"))))
    (build-system cmake-build-system)
    (native-inputs (list pkg-config libmodule))
    (inputs `(("elogind" ,elogind)
              ("libconfig" ,libconfig)
              ("popt" ,popt)
              ("libmodule" ,libmodule)
              ("dbus",dbus)
              ("bash-completion" ,bash-completion)
              ("gsl" ,gsl)))
    (propagated-inputs `(("geoclue" ,geoclue)
                         ("upower" ,upower)
                         ("clightd" ,clightd)
                         ("ddcutil" ,ddcutil)
                         ("bash-completion" ,bash-completion)))
    (arguments
     `(#:tests? #f ; tests not found
       #:phases
       (modify-phases %standard-phases
                      (add-after
                       'patch-source-shebangs 'patch-target-dirs
                       (lambda* (#:key outputs #:allow-other-keys)
                         (let* ((out (assoc-ref outputs "out")))
                           (substitute*
                            "./CMakeLists.txt"
                            (("DESTINATION \\$\\{SESSION_BUS_DIR\\}")
                             (string-append
                              "DESTINATION " out
                              "/share/dbus-1/services")))
                           (substitute*
                            "./CMakeLists.txt"
                            (("DESTINATION /etc/xdg/autostart")
                             (string-append
                              "DESTINATION " out
                              "/etc/xdg/autostart")))
                           (substitute*
                            "./CMakeLists.txt"
                            (("DESTINATION /usr/share/*")
                             (string-append "DESTINATION " out
                                            "/share/*")))
                           (substitute*
                            "./CMakeLists.txt"
                            (("DESTINATION \\$\\{BASH_COMPLETIONS_DIR\\}")
                             (string-append "DESTINATION "
                                            out "/share/bash-completion")))))))))
    (home-page "https://github.com/FedeDP/Clight")
    (synopsis "C user daemon utility that aims to fully manage your screens")
    (description "This is a tiny C utility that can turn your webcam into a
light sensor; moreover it supports a redshift-like gamma control, a screen
dimmer and dpms settings.")
    (license license:gpl3)))

(define-public clightd
  (package
    (name "clightd")
    (version "5.4")
    (source
     (origin
       (method git-fetch)
       (uri (git-reference
             (url "https://github.com/FedeDP/Clightd")
             (commit version)))
       (file-name (git-file-name name version))
       (sha256
        (base32 "04bl9jrh4msz792clhp0i4ascgghbx6w4gzprs7fdps1frzb35m6"))))
    (build-system cmake-build-system)
    (native-inputs (list pkg-config libmodule))
    (inputs
      `(("libxrandr" ,libxrandr)
        ("wayland-protocols" ,wayland-protocols)
        ("libdrm" ,libdrm)
        ("libjpeg-turbo" ,libjpeg-turbo)
        ("wayland" ,wayland)
        ("libmodule" ,libmodule)
        ("libx11" ,libx11)
        ("libxext" ,libxext)
        ("polkit" ,polkit)
        ("eudev" ,eudev)
        ("elogind" ,elogind)
        ("dbus" ,dbus)
        ("libusb" ,libusb)))
        ;;needed only for yoctolight support, but why not
    (propagated-inputs
     `(("ddcutil" ,ddcutil)))
    (arguments
     `(#:tests? #f  ; tests not found
       #:configure-flags '("-DENABLE_GAMMA=1"
                           "-DENABLE_DPMS=1"
                           "-DENABLE_DDC=1"
                           "-DENABLE_SCREEN=1"
                           "-DENABLE_YOCTOLIGHT=1") ; well, why not ;)
       #:phases
       (modify-phases %standard-phases
         (add-after
             'patch-source-shebangs 'patch-target-dirs
           (lambda* (#:key outputs #:allow-other-keys)
             (let* ((out (assoc-ref outputs "out")))
               (substitute*
                   "./CMakeLists.txt"
                 (("DESTINATION \\$\\{SYSTEM_BUS_DIR\\}")
                  (string-append "DESTINATION "
                                 out "/share/dbus-1/system-services")))
               (substitute*
                   "./CMakeLists.txt"
                 (("DESTINATION \\$\\{POLKIT_ACTION_DIR\\}")
                  (string-append "DESTINATION "
                                 out "/share/polkit-1/actions")))
               (substitute*
                   "./CMakeLists.txt"
                 (("DESTINATION \\$\\{MODULE_LOAD_DIR\\}")
                  (string-append "DESTINATION "
                                 out "/etc/modprobe.d")))
               (substitute*
                   "./CMakeLists.txt"
                 (("DESTINATION \\$\\{DBUS_CONFIG_DIR\\}")
                  (string-append "DESTINATION "
                                 out "/share/dbus-1/system.d"))))))
         (add-after 'build 'symlink-clightd-in-PATH
           (lambda _
             (mkdir-p (string-append
                       (assoc-ref %outputs "out") "/bin"))
             (symlink
              (string-append (assoc-ref %outputs "out") "/libexec/clightd")
              (string-append (assoc-ref %outputs "out") "/bin/clightd")))))))
    (home-page "https://github.com/FedeDP/Clight")
    (synopsis "Daemon dbus interface to for backlight and display settings")
    (description
"A linux bus interface that lets you change screen brightness, compute
captured webcam frames brightness and change screen temperature.")
    (license license:gpl3)))
